using System;
using webapi.Infrastructure.Repository;
using Microsoft.AspNetCore.Mvc;
using webapi.Infrastructure.Db;
using Webapi.Domain.Entity;
using System.Collections.Generic;
using webapi.Domain.Repository;
using System.Linq;
using Microsoft.Extensions.Logging;
using webapi.Infrastructure.Utils;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using System.IdentityModel.Tokens.Jwt;
using Newtonsoft.Json;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
// using JwtAuthDemo.Helpers;
using webapi.Infrastructure.Filters;
using Microsoft.Extensions.Configuration;
using webapi.Infrastructure.Parameter;

namespace webapi.api.Controllers
{
    [ServiceFilter(typeof(ExceptionFilter))]
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        // private readonly webapiDbContext _db;
        private readonly IRepository<Users> _users;
        private readonly ILogger<UsersController> _logger;
        private readonly IConfiguration _configuration;

        public UsersController(IRepository<Users> users, ILogger<UsersController> logger, IConfiguration configuration)
        {
            // _db = new webapiDbContext();
            // _users = new EFRepository<Users>();
            _users = users;
            _logger = logger;
            _configuration = configuration;
        }
        public IEnumerable<dynamic> Index()
        {
            // throw new Exception("出现了一个新的错误");
            // var users = _users.Table.ToArray();
            var lst = new List<dynamic>{
                new {Avatar="888",Money=250},
                new {Avatar="中国人民银行",Money=250},
                new {Product="5555",Money=250},
                new {Avatar="333",Height=250},
            };
            _logger.LogInformation(lst.JsonSerialize());
            return lst;
        }

        [HttpPost]
        public Users CreateUser(Users entity)
        {
            System.Console.WriteLine(entity);
            var user =
                new Users
                {
                    UserName = entity.UserName,
                    Password = entity.Password
                };
            var list = _users.Add(user);
            return list;
        }

        [HttpPut]
        [Route("{id}")]
        public Users update(Guid id, Users entity)
        {
            var user = _users.getById(id);
            if (user != null)
            {
                user.UserName = entity.UserName;
                user.Password = entity.Password;
                _users.Update(user);
            }
            return user;
        }

        [HttpDelete]
        [Route("{id}")]
        public Users deleteUser(Guid id)
        {
            var user = _users.getById(id);
            if (user != null)
            {
                user.IsDeleted = true;
                _users.Update(user);
            }
            return user;
        }

        [AllowAnonymous]
        [Route("/token")]
        public string Login()
        {
            var username = "admin";
            var rolename = "超级管理";
            // var password="113";

            var tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();

            var token = TokenHelper.GenerateToken(tokenParameter, username, rolename);
            var res = new
            {
                Code = 1000,
                Msg = "登录成功",
                Data = token
            };
            return res.JsonSerialize();
        }

        
    }
    // public class UserCto
    // {
    //     public string UserName { get; set; }

    //     public string Password { get; set; }

    //     public bool IsActived { get; set; }

    //     public bool IsDeleted { get; set; }
    // }
}